#!/usr/bin/python

######   Dependency Injection - START   ######
import sys
import os
import logging
import xmlrpclib

current_dir=os.path.dirname(os.path.abspath(__file__))

config_dir=current_dir+"/config/"
actions_dir=current_dir+"/actions/"
libs_dir=current_dir+"/libs/"
logs_dir=current_dir+"/logs/"
reports_dir=current_dir+"/reports/"

sys.path.append(config_dir) #custom path
sys.path.append(actions_dir) #custom path
sys.path.append(libs_dir) #custom path


import configuration #custom module
import decrypt #custom module
import functions #custom module
import report #custom module

config=configuration.params #configuration instance
crypto = decrypt.Crypto() #password decoder instance

LOG_FILENAME = logs_dir+'CertCollector.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
#logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO)
#logging.basicConfig(filename=LOG_FILENAME,level=logging.ERROR)

#Create list of uniq actions in configuration
#actions=list()
#for key in config.keys(): 
#    actions.append(config[key]["action_name"])

#actions = functions.Custom().list_uniq(actions)

logging.info(" %s  Dependency injections successfully finished." % functions.Custom().cTime())
#logging.info(" %s  " % functions.custom().ctime);


######   Dependency Injection - END   ######


######   Actions Execution - START   ######

# Instantiate rpc client
rpc_client = xmlrpclib.ServerProxy('http://localhost:2222')

for key in config.keys(): #Loop through configuration and execute actions
    numberOfParams=len(config[key]) -1
    params=list()
    count=1
    while (count <= numberOfParams):
        params.append(config[key]["action_name"])
        count = count + 1
    
#    print "For server: " + key + " => action: " + config[key]["action_name"] + " Password: " + crypto.decode(config[key]["hop1_pass"])

    print rpc_client.run_action(config[key]["action_name"], key, config[key])

######   Actions Execution - END   ######



######   Reports Generation - START   ######

# Tuple example to be provided to report
tpl=(
     ["host 1","Subject 1","Valid_to 1","Is Expired 1"],
     ["host 2","Subject 2","Valid_to 2","Is Expired 2"],
     ["host 3","Subject 3","Valid_to 3","Is Expired 3"]
     )

report.execute().createExcelReport(tpl,reports_dir)


######   Reports Generation - END   ######